﻿using System;
using System.IO;
using Newtonsoft.Json;
using Teigha.Core;
using Teigha.TD;

namespace OdaOperation.Service
{
    public class HostAppServices : ExHostAppServices
    {
        private static string configPath = AppDomain.CurrentDomain.RelativeSearchPath + "\\cadConfig.json";

        private string m_acadResource = "";

        private string m_matsPath = "";

        private string m_textureMapFile = "";

        public HostAppServices()
        {
            if (File.Exists(configPath))
            {
                Config config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(configPath));
                m_acadResource = config.CadResource;
                m_matsPath = config.MatsPath;
                m_textureMapFile = config.TextureMapFile;
            }
            else
            {
                Config value = new Config();
                File.WriteAllText(configPath, JsonConvert.SerializeObject(value));
            }
        }

        public override string findFile(string filename, OdRxObject pDb, FindFileHint hint)
        {
            try
            {
                string text = base.findFile(filename, pDb, hint);
                if (text.Contains("\\") || text.Contains("/"))
                {
                    return text;
                }

                if (string.IsNullOrWhiteSpace(filename))
                {
                    return text;
                }

                string text2 = filename;
                string text3 = new FileInfo(text2).Extension.ToUpper();
                switch (hint)
                {
                    default:
                        return text;
                    case FindFileHint.kFontFile:
                    case FindFileHint.kCompiledShapeFile:
                    case FindFileHint.kTrueTypeFontFile:
                    case FindFileHint.kPatternFile:
                    case FindFileHint.kFontMapFile:
                        if (text3 != ".SHX" && text3 != ".PAT" && text3 != ".TTF" && text3 != ".TTC" && text3 != ".OTF" && text3 != ".LIN")
                        {
                            text2 += ".shx";
                        }

                        break;
                    case FindFileHint.kTextureMapFile:
                        break;
                }

                text = ((hint != FindFileHint.kTextureMapFile) ? m_textureMapFile : m_matsPath);
                if (hint == FindFileHint.kTextureMapFile)
                {
                    return text;
                }

                text = m_acadResource;
                text += "\\Fonts\\";
                text += text2;
                if (!File.Exists(text))
                {
                    text = m_acadResource;
                    text += "Support\\";
                    text += text2;
                    if (!File.Exists(text))
                    {
                        text = m_acadResource;
                        text += "UserDataCache\\Support\\";
                        text += text2;
                        if (!File.Exists(text))
                        {
                            if (text2 == "GBHZFS.shx")
                            {
                                text = text2;
                            }
                            else
                            {
                                text = m_acadResource;
                                text += "Fonts\\gbcbig.shx";
                            }
                        }
                    }
                }

                return text;
            }
            catch (Exception)
            {
                return filename;
            }
        }
    }
}